from loupe.perceptron import Perceptron
from random import random
import unittest


class testPerceptron(unittest.TestCase):

    def setUp(self):
        self.default_inputs = [0,0,0,0,0,0,0,0,0,0,0,0]
        self.default_outputs = [0,0,0,0]
        self.pc = Perceptron(len(self.default_inputs),
                             len(self.default_outputs))
        
    def test_inputs(self):
        self.assertEqual(self.pc.inputs, self.default_inputs)
        
    def test_outputs(self):
        self.assertEqual(self.pc.outputs, self.default_outputs)

    def test_size_of_weights(self):
        self.assertEqual(len(self.pc.weights), len(self.default_outputs))
        for wi in self.pc.weights:
            self.assertEqual(len(wi), len(self.default_inputs))
            
    def test_val_of_weights(self):
        for i in xrange(len(self.default_outputs)):
            for j in xrange(len(self.default_inputs)):
                self.assertTrue(self.pc.weights[i][j] > 0)
                self.assertTrue(self.pc.weights[i][j] < 0.1)
                
    def test_call_image(self):
        image = self._create_image(len(self.default_inputs))
        self.pc.call(image)
        self.assertEqual(self.pc.inputs, image)

    def test_remeber_image(self):
        image1 = self._create_image(len(self.default_inputs))
        res1 = [0,0,0,1]
        self.pc.remember(image1, res1)
        self.assertEqual(self.pc.call(image1), res1)

        image2 = self._create_image(len(self.default_inputs))
        res2 = [1,0,0,0]
        self.pc.remember(image2, res2)
        self.assertEqual(self.pc.call(image2), res2)

    def _create_image(self, size):
        return [ random() for x in xrange(size) ]



if __name__ == '__main__':
    unittest.main()
